﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows;

namespace eipch
{
    class Templates
    {

        public const String COL_Name = "Name";
        public const String COL_IP = "IP-Adresse";
        public const String COL_NM = "Netzmaske";
        public const String COL_GW = "Gateway";
        public const String COL_DNS = "DNS";
        public const String COL_DHCP = "DCHP";
        public const String TableName = "dtTemplatesMain";

        public System.Data.DataTable dt;

        /// <summary>
        /// Konstruktor, erzeugt die Datatable
        /// </summary>
        public Templates()
        {
            dt = new DataTable(TableName);
            dt.Columns.Add(COL_Name);
            dt.Columns.Add(COL_IP);
            dt.Columns.Add(COL_NM);
            dt.Columns.Add(COL_GW);
            dt.Columns.Add(COL_DNS);
            dt.Columns.Add(COL_DHCP);
        }

        /// <summary>
        /// Speichert die Datatable in einer XML Datei
        /// </summary>
        /// <param name="FileName"></param>
        public void Save(String FileName)
        {
            dt.WriteXml(FileName);
        }

        /// <summary>
        /// Lädt die Datatable von einer XML Datei
        /// </summary>
        /// <param name="FileName"></param>
        public void Load(String FileName)
        {
            dt.Rows.Clear();
            dt.ReadXml(FileName);
        }

        /// <summary>
        /// Gibt eine Liste von Templates zurück
        /// </summary>
        /// <returns></returns>
        public List<String> GetTemplatesList()
        {
            List<String> returnList = new List<String>();

            foreach (DataRow row in dt.Rows)
            {
                returnList.Add(row.Field<String>(COL_Name));
            }
            return returnList;
        }

        public NetworkConfiguration getSettings(String templateName)
        {
            NetworkConfiguration actSettings = null;

            foreach (DataRow row in dt.Rows)
            {
                try
                {
                    if (row.Field<String>(COL_Name).Equals(templateName))
                    {

                        actSettings = new NetworkConfiguration()
                        {
                            ipAddress = new IPAddress(row.Field<String>(COL_IP).ToString()),
                            netMask = new IPAddress(row.Field<String>(COL_NM).ToString()),
                            gateway = new IPAddress(row.Field<String>(COL_GW).ToString()),
                            dns = IPAddress.generateIPAddressList(row.Field<String>(COL_DNS).ToString().Split(',')),

                            dhcp = false,
                            isSet = true
                        };

                        if (row.Field<String>(COL_DHCP).Equals("true"))
                            actSettings.dhcp = true;
                    }
                }
                catch (NullReferenceException)
                {
                    MessageBox.Show("Template konnte nicht geladen werden", "Fehler");
                }

            }

            return actSettings;
        }
    }
}
